// pages/cart/cart.js
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    goods: [],
    goodsNumber: [],
    checkedList: [],
    price: 0,
    show: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    //this.getGoodsDetail()
    //console.log(app.globalData.cartList)
    this.setData({
      goods: app.globalData.cartList, //获取购物车列表数据
      goodsNumber: app.globalData.goodsNumber, //获取商品数量
      checkedList: app.globalData.checkedList //获取选中状态
    })
    if (this.data.goods.length > 0) {
      this.setData({
        show: false
      })
    }
    this.addSum()
  },
  onHide() {
    //wx.setStorageSync('cartList', app.globalData.cartList)
    //app.globalData.goodsNumber.push(this.data.goodsNumber)
  },
  //商品数量+1
  add(event) {
    let index = event.currentTarget.dataset.number
    this.data.goodsNumber[index] = this.data.goodsNumber[index] + 1
    this.setData({
      goodsNumber: this.data.goodsNumber
    })
    this.addSum()
  },
  //商品数量-1
  reduce(event) {
    let index = event.currentTarget.dataset.number
    if (this.data.goodsNumber[index] > 1)
      this.data.goodsNumber[index] = this.data.goodsNumber[index] - 1
    this.setData({
      goodsNumber: this.data.goodsNumber
    })
    this.addSum()
  },
  //跳转到商品详情页
  toGoodsDetail(event) {
    let id = event.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/goodsDetail/goodsDetail?id=' + id,
    })
  },
  //是否选择该商品
  select(event) {
    let index = event.currentTarget.dataset.index
    this.data.checkedList[index] = !this.data.checkedList[index]
    this.setData({
      checkedList: this.data.checkedList
    })
    this.addSum()
  },
  //计算商品价格(加)
  addSum() {
    let sum = 0
    for (let i = 0; i < this.data.goods.length; i++) {
      let temp = this.data.goods[i].price * this.data.goodsNumber[i]
      //console.log(temp)
      if (this.data.checkedList[i]) {
        sum = sum + temp * 100
      }
    }
    this.setData({
      price: sum
    })
  },
  //跳转到订单页面
  onSubmit() {
    let orderList = []
    // orderList.push(this.data.goods)
    for (let i = 0; i < this.data.goods.length; i++) {
      if (this.data.checkedList[i]) {
        this.data.goods[i].need = this.data.goodsNumber[i]//添加进真实订单列表中
        this.data.goods[i].checked = this.data.checkedList[i]
        orderList.push(this.data.goods[i])
      }
    }
    app.globalData.orderList = orderList
    if (orderList.length > 0) {
      wx.navigateTo({
        url: '/pages/order/order?price=' + this.data.price,
      })
    } else {
      wx.showToast({
        title: '还没有任何商品',
        icon: 'error'
      })
    }
  },
  delete(event) {
    let index = event.currentTarget.dataset.index
    //下方这三个console是必要的，是为了执行splice语句
    console.log(this.data.goods.splice(index,1))
    console.log(this.data.goodsNumber.splice(index,1))
    console.log(this.data.checkedList.splice(index,1))
    this.setData({
      goods: this.data.goods,
      goodsNumber: this.data.goodsNumber,
      checkedList: this.data.checkedList
    })
    this.addSum()
    app.globalData.cartList =  this.data.goods//获取购物车列表数据
    app.globalData.goodsNumber =  this.data.goodsNumber//获取商品数量
    app.globalData.checkedList = this.data.checkedList//获取选中状态
    wx.setStorageSync('cartList', app.globalData.cartList)
    wx.setStorageSync('goodsNumber', app.globalData.goodsNumber)
    wx.setStorageSync('checkedList', app.globalData.checkedList)

    //设置背景图
    //console.log(this.data.goods.length)
    if(this.data.goods.length == 0)
      this.setData({
        show: true
      })
  },
  onShareAppMessage(res){
    let myObj = {
      title:'琪琪的手工饰品屋',
      path:"/pages/cart/cart",
    }
    return myObj;
  }
})